package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcRfidReaderLocationMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcRfidReaderLocation;
import com.csun.cmny.provider.model.dto.propertyManagement.SelectDeviceDataDto;
import com.csun.cmny.provider.model.dto.rfid.BindDmcRfidReaderDto;
import com.csun.cmny.provider.model.dto.rfid.RfidReaderPageQuery;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyDataVo;
import com.csun.cmny.provider.model.vo.rfid.DmcRfidReaderVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcRfidEventOldmanService;
import com.csun.cmny.provider.service.DmcRfidReaderLocationService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-21 17:12
 * @Description: the class dmc rfid reader location service impl
 */
@Service
public class DmcRfidReaderLocationServiceImpl extends BaseService<DmcRfidReaderLocation>
        implements DmcRfidReaderLocationService{

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private DmcRfidReaderLocationMapper dmcRfidReaderLocationDao;
    @Resource
    private DmcRfidEventOldmanService dmcRfidEventOldmanService;

    /**
     * bind dmc rfid reader
     *
     * @param bindDmcRfidReaderDto bind dmc rfid reader dto
     *
     * @return the int
     */
    @Override
    public Integer bindDmcRfidReader(BindDmcRfidReaderDto bindDmcRfidReaderDto) {

        // 1.判断设备Id是否存在
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsRfidReaderByDeviceId(bindDmcRfidReaderDto.getDeviceId());
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610016, bindDmcRfidReaderDto.getDeviceId());
        }

        // 2.校验位置是否为空
        Preconditions.checkArgument(!PubUtils.isNull(bindDmcRfidReaderDto.getLocation()),
                cmnyMessageSource.getMessage("dmc.rfid.reader.location.null", null));

        // 3.判断门禁读卡器是否已经绑定
        DmcRfidReaderLocation dmcRfidReaderLocation = mapper.selectByPrimaryKey(bindDmcRfidReaderDto.getDeviceId());
        if (PublicUtil.isNotEmpty(dmcRfidReaderLocation)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610008, dmcRfidReaderLocation.getDeviceId(),
                    dmcRfidReaderLocation.getLocation());
        }

        // 4.绑定门禁读卡器
        dmcRfidReaderLocation = new DmcRfidReaderLocation();
        BeanUtils.copyProperties(bindDmcRfidReaderDto, dmcRfidReaderLocation);
        Integer result = dmcRfidReaderLocationDao.insertSelective(dmcRfidReaderLocation);

        return result;
    }

    /**
     * break dmc rfid reader
     *
     * @param deviceId device id
     *
     * @return the int
     */
    @Override
    public Integer breakDmcRfidReader(Long deviceId) {

        // 1.判断设备Id是否存在
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsRfidReaderByDeviceId(deviceId);
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610016, deviceId);
        }

        // 2.判断门禁读卡器是否已经绑定
        DmcRfidReaderLocation dmcRfidReaderLocation = mapper.selectByPrimaryKey(deviceId);
        if (PublicUtil.isEmpty(dmcRfidReaderLocation)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610009, deviceId);
        }

        // 3.解绑门禁读卡器
        Integer result = dmcRfidReaderLocationDao.deleteByPrimaryKey(deviceId);

        /**
         * @date    2019-12-13
         * @author  chisj
         * @desc    解绑时清除门禁事件记录
         */
        if (result > 0) {
            dmcRfidEventOldmanService.deleteByReaderId(deviceId);
        }

        return result;
    }

    /**
     * query rfid reader list with page
     *
     * @param rfidReaderPageQuery rfid reader page query
     *
     * @return the page info
     */
    @Override
    public PageInfo queryRfidReaderListWithPage(RfidReaderPageQuery rfidReaderPageQuery) {

        PageHelper.startPage(rfidReaderPageQuery.getPageNum(), rfidReaderPageQuery.getPageSize());

        List<DmcRfidReaderVo> dmcRfidReaderVoList = dmcRfidReaderLocationDao
                .queryRfidReaderListWithPage(rfidReaderPageQuery);

        return new PageInfo<>(dmcRfidReaderVoList);
    }

    /**
     * select rfid reader by rfid reader id
     *
     * @param deviceId the device id
     *
     * @return the dmc rfid reader vo
     */
    @Override
    public DmcRfidReaderVo selectByRfidReaderId(Long deviceId) {

        return dmcRfidReaderLocationDao.selectByRfidReaderId(deviceId);
    }

    /**
     * select rfid reader by rfid reader no
     *
     * @param readerNo the rfid reader no
     *
     * @return the dmc rfid reader vo
     */
    @Override
    public DmcRfidReaderVo selectByRfidReaderNo(String readerNo) {

        return dmcRfidReaderLocationDao.selectByRfidReaderNo(readerNo);
    }

    @Override
    public List<PropertyDataVo> selectDeviceData(SelectDeviceDataDto selectDeviceDataDto) {
        return dmcRfidReaderLocationDao.selectDeviceData(selectDeviceDataDto);
    }
}
